<?php
class Lead_EDO_Collection_Pager extends Lead_Event_Observable_Abstract implements IteratorAggregate
{
    
    /**
     * Collection object
     *
     * @var Lead_EDO_Collection_Abstract
     */
    private $_collection;   
  	
    private $_page = 1;
	
    private $_pageSize = 10;
    
    public function __construct(Lead_EDO_Collection_Abstract $collection, $pageSize = 10, $page = 1)
    {
        $this->_collection = $collection;
        $this->_pageSize = $pageSize;
        $this->_page = $page;
    }

    
    public function extractItems()
    {
        $start = ($this->_page - 1) * $this->_pageSize;
        $end = $start + $this->_pageSize;
        	        
        $items = array_slice($this->_collection->getKeys(), $start, $this->_pageSize);
        
        return $items;   
    }

    
    
        
    public function getIterator()
    {
        return $this->_collection->getClone($this->extractItems());
    }

   
    public function setPage($page)
    {
        if($page <= $this->getNumberOfPages())
            $this->_page = $page;
    }
    
    public function getNumberOfPages()
    {
        $numberOfPages = (ceil(count($this->_collection->getKeys()) / $this->_pageSize));	
        return $numberOfPages;
    }
    
    public function getPage()
    {
        return $this->_page;
    }
    
    
    public function setPageSize($pageSize)
    {
        $this->_pageSize = $pageSize;
    }
    
    
    public function count()
    {
        return $this->_collection->count();
    }
    
    
}
